﻿using System.Configuration;
using com.bodurov.SessionRecovery.Interfaces;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace com.bodurov.SessionRecovery.Tests.Utils
{
    public class TestHolder
    {
        private static readonly IUnityContainer _parentContainer;
        private static readonly IUnityContainer _container;

        static TestHolder()
        {
            _parentContainer = new UnityContainer();

            _parentContainer.RegisterType<ISessionRecoveryConfiguration, SessionRecoveryConfiguration>(
                new ContainerControlledLifetimeManager());
            _parentContainer.RegisterType<ISessionRecoveryLogger, SessionRecoveryLogger>(
                new ContainerControlledLifetimeManager());
            _parentContainer.RegisterType<ISessionRecoveryManager, SessionRecoveryManager>(
                new ContainerControlledLifetimeManager());
            _parentContainer.RegisterType<ISessionRecoverySerializer, SessionRecoverySerializer>(
                new ContainerControlledLifetimeManager());
            _parentContainer.RegisterType<ISessionRecoveryStorageManager, SessionRecoveryStorageManager>(
                new ContainerControlledLifetimeManager());

            _container = _parentContainer.CreateChildContainer();

            var section = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;

            if(section != null)
            {
                var current = section.Containers["SessionRecovery"];
                if (current != null)
                {
                    current.Configure(_container);
                }
            }
        }
        public static ISessionRecoveryConfiguration Config
        {
            get { return _container.Resolve<ISessionRecoveryConfiguration>(); }
        }
        public static ISessionRecoveryLogger Logger
        {
            get { return _container.Resolve<ISessionRecoveryLogger>(); }
        }
        public static ISessionRecoveryManager Manager
        {
            get { return _container.Resolve<ISessionRecoveryManager>(); }
        }
        public static ISessionRecoverySerializer Serializer
        {
            get { return _container.Resolve<ISessionRecoverySerializer>(); }
        }
        public static ISessionRecoveryStorageManager Storage
        {
            get { return _container.Resolve<ISessionRecoveryStorageManager>(); }
        }
    }
}